<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="yongjian.drp.model.User" %>
<%@ taglib uri="/struts-tags" prefix="s" %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

User user = (User)session.getAttribute("user");
if(!user.getPosition().equals("root")){
	out.write("<center><img src=../../imgs/lock.jpg /><br>");
	out.write("你无权操作</center>");
	return;
}
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'user_add.jsp' starting page</title>
    
    <link rel="stylesheet" type="text/css" href="js/themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="js/themes/icon.css">
    <script type="text/javascript" src="js/jquery.min.js"></script>
	<script type="text/javascript" src="js/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="js/locale/easyui-lang-zh_CN.js"></script>
    
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<script type="text/javascript">
		$(function(){
			 $.extend($.fn.validatebox.defaults.rules, {   	
			      minLength: {   	
			          validator: function(value, param){   	
			              return value.length >= param[0];   	
			          },   	
			          message: ''  	
			      }  ,
			      equalTo:{
			    	  validator:function(value,param){
			    		 return $(param[0]).val() == value;
			    	  },
			    	  message:''
			      }
			  });
			 
			  $('#grid').datagrid({   
				  	  idField:'id',
				      url:'json/user_list.action', 
				      striped:true,
				      loadMsg:'正在加载中，请等待....',
				      pagination:true,
				      fitColumns:true,
				      rownumbers:true,
				      pageSize:7,
				      pageList:[7,14,21,28,35],
				      
				      frozenColumns:[
										[{
											field:'cb',
											checkbox:true									
										 }]				               
						               ],
				      columns:[[   
				          {field:'username',title:'用户名',width:100},   
				          {field:'address',title:'地址',width:100},   
				          {field:'tel',title:'电话',width:100},   
				          {field:'email',title:'邮箱',width:100},  
				          {field:'createDate',title:'创建日期',width:100},
				          {field:'position',title:'职位',width:100,
				        	  formatter:function(value,record,index){
					        		if(value == 'root'){
					        			return '管理员';
					        		} else if(value == 'manager'){
					        			return '经理';
					        		} else if(value = 'recorder'){
					        			return '录入员';
					        		} else {
					        			return '会计';
					        		}
			        			}},
				      ]] ,
				      toolbar:[{
							text:'删除用户',
							iconCls:'icon-remove',
							handler:function(){
								var arr = $('#grid').datagrid('getSelections');//得到所有选中的项
								if(arr.length < 1){
									$.messager.alert('提示','至少选择一个用户编辑','warning');
								} else {
									 $.messager.confirm('确认','确定删除所选记录吗？',function(r){   
										       if (r){   
										           var idStr = '';
										           for(var i=0;i < arr.length;i++ ){
										        		 idStr += arr[i].id + ',';
										        	 };
										           idStr = idStr.substring(0,idStr.length-1);
										           $.post('json/user_delete.action',
										        		   {idStr:idStr},
										        		   function(){
										        			   $('#grid').datagrid('reload');			//1、重载
										        			   $('#grid').datagrid('clearSelections');	//2、清除所有选中
										        			   $.messager.show({						//3、提示
													        		title:'提示',
													        		height:170,
													        		msg:'删除成功'
													        	});
										        		   });
										       }   
										   }); 
								}
							}
						},{
							text:'编辑用户',
							iconCls:'icon-edit',
							handler:function(){
								var arr = $('#grid').datagrid('getSelections');
								if(arr.length == 1){
									$('#myform').get(0).reset();
									$('#dialog').dialog('open');
									$('#myform').form('load',{		//重载表单
										"user.id":arr[0].id,
										"user.username":arr[0].username,
										"user.email":arr[0].email,
										"user.tel":arr[0].tel,
										"user.address":arr[0].address,
										"user.position":arr[0].position
									});
								} else {
									$.messager.alert('提示','请选择一个用户编辑','warning'); 
								}
							}
						}]
				  });
			  $('#btn').click(function(){
				    $('#myform').form('submit', {   
					        url:'json/user_modify.action',   
					        onSubmit: function(){  
					        	if(!$('#myform').form('validate')){
					        		return false;
					        	}
					        },   
					        success:function(){  
					        	$('#dialog').dialog('close');
					        	$('#grid').datagrid('reload');
					        	$.messager.show({
					        		title:'提示',
					        		height:170,
					        		msg:'修改成功'
					        	});
					       }   
					   });
			  });
			  
			  $('#search').searchbox({   
				     width:200,   
				     searcher:function(value){   
				         $('#grid').datagrid('load',{key:value});  
				     },     
				     prompt:'请输入关键字'  
				 }); 
		});
	</script>

  </head>
  
  <body>
  		关键字：<input id="search" name="key"></input>
  		<br><br><br>
		<table id="grid"></table>  	 		 	 	 	
		<div id="dialog" class="easyui-dialog" title="编辑用户" draggable=false closed=true resizable=false modal=true style="width:300px;">  	     	 
			 <form id="myform" action="" method="post">
				 <input type="hidden" name="user.id" value=""/>
			  		<table>
			  			<tr>
			  				<td>用户名</td>
			  				<td>
			  					<input type="text" name="user.username" class="easyui-validatebox" required=true 
			  						missingMessage="用户名必填" validType="minLength[3]" invalidMessage="用户名至少3个字符"
			  						style="width:200px;">
			  				</td>
			  			</tr>			  			
			  			<tr>
			  				<td>邮箱</td>
			  				<td>
			  					<input type="text" name="user.email" class="easyui-validatebox" required=true 
			  						missingMessage="邮箱必填" validType="email" invalidMessage="邮箱格式错误"
			  						style="width:200px;">
			  				</td>
			  			</tr>
			  			<tr>
			  				<td>电话</td>
			  				<td>
			  					<input type="text" name="user.tel" class="easyui-validatebox" required=true 
			  						missingMessage="联系电话必填" style="width:200px;">
			  				</td>
			  			</tr>
			  			<tr>
			  				<td>地址</td>
			  				<td>
			  					<input type="text" name="user.address" class="easyui-validatebox" required=true 
			  						missingMessage="地址必填" style="width:200px;">
			  				</td>
			  			</tr>
			  			<tr>
			  				<td>职位</td>
			  				<td>
			  					 <select name="user.position" style="width:200px;" class="easyui-validatebox" 
			  					 		required=true missingMessage="职位必填">  
								      <option value="manager">经理</option>  					
								      <option value="recorder" selected="selected">录入员</option>  					
								      <option value="accountant">会计员</option>  											
								  </select> 
			  				</td>
			  			</tr>
			  			<tr>
			  				<td></td>
			  				<td align="center">
			  					<a id="btn" class="easyui-linkbutton">保存</a>
			  				</td>
			  			</tr>
			  		</table>	
			  	</form>	
		  	</div> 
  	
  </body>
</html>
